package com.food.servlet;

import com.food.dao.CategoryDAO;
import com.food.dao.MenuDAO;
import com.food.model.Category;
import com.food.model.Menu;
import com.food.util.FileUploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/MenuServlet")
//上传文件
@MultipartConfig // 必须声明这个注解来支持文件上传
public class MenuServlet extends HttpServlet {
    private MenuDAO menuDAO = new MenuDAO();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        String search = request.getParameter("search") != null ? request.getParameter("search") : "";
        int page = request.getParameter("page") != null ? Integer.parseInt(request.getParameter("page")) : 1;
        int pageSize = 3;

        try {
            if ("delete".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                menuDAO.deleteMenu(id);
                response.sendRedirect("MenuServlet");
                return;
            }

            int totalCount = menuDAO.getTotalCount(search);
            int totalPages = (int) Math.ceil((double) totalCount / pageSize);
            List<Menu> menus = menuDAO.getMenus(search, page, pageSize);

            request.setAttribute("menus", menus);
            request.setAttribute("totalPages", totalPages);
            request.setAttribute("currentPage", page);
            request.getRequestDispatcher("menumanage.jsp").forward(request, response);
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");
        try {
            if ("add".equals(action)) {
                String name = request.getParameter("name");
                int categoryid = Integer.parseInt(request.getParameter("categoryid"));
                CategoryDAO categoryDAO=new CategoryDAO();
                Category categoryById = categoryDAO.getCategoryById(categoryid);
                String categoryname = categoryById.getName();
                double price = Double.parseDouble(request.getParameter("price"));
                String description = request.getParameter("description");


                //文件路径设置为当前类路径下
                String uploadDir = getServletContext().getRealPath("/uploads");
                Part filePart = request.getPart("pic");
//使用工具类保存文件
                String filepath= FileUploadUtil.uploadFile(uploadDir, filePart);

                Menu menu = new Menu();
                menu.setName(name);
                menu.setCategoryid(categoryid);
                menu.setCategoryname(categoryname);
                menu.setPrice(price);
                menu.setPic(filepath);
                menu.setDescription(description);
                menuDAO.addMenu(menu);
            } else if ("edit".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                String name = request.getParameter("name");
                int categoryid = Integer.parseInt(request.getParameter("categoryid"));
                String categoryname = request.getParameter("categoryname");
                double price = Double.parseDouble(request.getParameter("price"));
                String description = request.getParameter("description");

                //文件路径设置为当前类路径下
                String uploadDir = getServletContext().getRealPath("/uploads");
                Part filePart = request.getPart("pic");
//使用工具类保存文件
                String filepath= FileUploadUtil.uploadFile(uploadDir, filePart);

                Menu menu = new Menu();
                menu.setId(id);
                menu.setName(name);
                menu.setCategoryid(categoryid);
                menu.setCategoryname(categoryname);
                menu.setPrice(price);
                menu.setPic(filepath);
                menu.setDescription(description);
                menuDAO.updateMenu(menu);
            }
            response.sendRedirect("MenuServlet");
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }
}
